//
//  HotCoinChartView.m
//  FindClass
//
//  Created by tianlong on 2024/12/4.
//

#import "HBHotCoinChartView.h"
#import "HBHotCoinChartLineView.h"
#import "HBHotCoinChartXAxisView.h"
#import "UIView+Extension.h"
#import "HBHotCoinChartUtils.h"

@interface HBHotCoinChartView ()

@property (nonatomic, weak) HBHotCoinChartLineView *lineView;
@property (nonatomic, weak) HBHotCoinChartXAxisView *xView;
/// 横0
@property (nonatomic, strong) CALayer *layer0;
/// 横1
@property (nonatomic, strong) CALayer *layer1;
/// 横2
@property (nonatomic, strong) CALayer *layer2;
/// 横3
@property (nonatomic, strong) CALayer *layer3;
/// 竖线
@property (nonatomic, strong) CALayer *layer4;
/// 圆环
@property (nonatomic, strong) CALayer *circleLayer;
/// 悬浮框
@property (nonatomic, strong) HBHotCoinChartPopupView *popup;

@end

@implementation HBHotCoinChartView

static CGFloat xTop = 4;
static CGFloat chartPadding = 20;
static CGFloat chartLineH = 0.5;

- (instancetype)initWithChartModel:(HBHotCointChartModel *)chartModel {
    self = [super initWithChartModel:chartModel];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat xHeight = self.chartModel.xTextHeight.floatValue;
    self.lineView.frame = CGRectMake(0, chartPadding, self.width, self.height - chartPadding * 2 - xTop - xHeight);
    self.xView.frame = CGRectMake(0, CGRectGetMaxY(self.lineView.frame) + xTop + chartPadding, self.width, xHeight);
    
    CGFloat lineH = 0.5;
    CGFloat offY = (self.height - xTop - self.xView.height) / 3.0;
    self.layer0.frame = CGRectMake(0, 0, self.width, lineH);
    self.layer1.frame = CGRectMake(0, offY, self.width, lineH);
    self.layer2.frame = CGRectMake(0, offY * 2, self.width, lineH);
    self.layer3.frame = CGRectMake(0, self.height - xTop - self.xView.height, self.width, lineH);
}

- (void)setupUI {
    if (!self.layer0) {
        self.layer0 = [[CALayer alloc] init];
        self.layer0.backgroundColor = UIColor.blackColor.CGColor;// kColorEBEBEB,
        [self.layer addSublayer:self.layer0];
    }
    
    if (!self.layer1) {
        self.layer1 = [[CALayer alloc] init];
        self.layer1.backgroundColor = UIColor.blackColor.CGColor;
        [self.layer addSublayer:self.layer1];
    }
    
    if (!self.layer2) {
        self.layer2 = [[CALayer alloc] init];
        self.layer2.backgroundColor = UIColor.blackColor.CGColor;
        [self.layer addSublayer:self.layer2];
    }
    
    if (!self.layer3) {
        self.layer3 = [[CALayer alloc] init];
        self.layer3.backgroundColor = UIColor.blackColor.CGColor;
        [self.layer addSublayer:self.layer3];
    }
    
    HBHotCoinChartLineView *lineView = [[HBHotCoinChartLineView alloc] initWithChartModel:self.chartModel];
    [lineView setClickedPoint:^(CGPoint p, HBHotCointChartItemModel *item) {
        if (self.circleLayer) {
            [self.circleLayer removeFromSuperlayer];
        }
        if (self.layer4) {
            [self.layer4 removeFromSuperlayer];
        }
        if (self.popup) {
            [self.popup removeFromSuperview];
        }
        
        // 添加竖线
        self.layer4 = [[CALayer alloc] init];
        self.layer4.frame = CGRectMake(p.x, 0, chartLineH, self.height - xTop - self.xView.height);
        self.layer4.backgroundColor = UIColor.orangeColor.CGColor;
        [self.layer addSublayer:self.layer4];
        
        // 添加圆环,fillColor=baseColorMajorTheme100,borderColor=KBaseColorContentBackground
        CGFloat circleLayerR = 3;
        self.circleLayer = [HBHotCoinChartUtils drawCircleInView:self centerPoint:CGPointMake(p.x , p.y + chartPadding) radius:circleLayerR fillColor:UIColor.blueColor borderColor:UIColor.whiteColor borderWidth:1];
        [self.layer addSublayer:self.circleLayer];
        
        if (!self.popup) {
            self.popup = [[HBHotCoinChartPopupView alloc] init];
            self.popup.layer.cornerRadius = 4;
            self.popup.layer.masksToBounds = YES;
        }
        self.popup.itemModel = item;
        CGFloat margin = 7; // 圆与popup的间距
        CGFloat popupX = p.x <= self.width * 0.5 ? p.x + margin : p.x - margin - self.popup.width;
        CGFloat padding = self.popup.height * 0.5 - chartPadding;//顶部保护距离
        CGFloat popupY = p.y + chartPadding - self.popup.height * 0.5;
        if (popupY <= padding) {
            popupY = 0;// 顶部保护
        }
        /*
        else if (popupY >= self.height - padding - self.popup.height) {
            popupY = self.height - padding - self.popup.height;// 底部保护，
        }*/
        self.popup.layer.frame = CGRectMake(popupX, popupY, self.popup.width, self.popup.height);
        [self.layer addSublayer:self.popup.layer];
    }];
    [self addSubview:lineView];
    self.lineView = lineView;
    
    HBHotCoinChartXAxisView *xView = [[HBHotCoinChartXAxisView alloc] initWithChartModel:self.chartModel];
    [self addSubview:xView];
    self.xView = xView;
}

- (void)reload:(HBHotCointChartModel *)model {
    [super reload:model];
    [self.lineView removeFromSuperview];
    [self.xView removeFromSuperview];
    [self.layer4 removeFromSuperlayer];
    [self.circleLayer removeFromSuperlayer];
    [self.popup removeFromSuperview];
    [self setupUI];
}

@end
